// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    // const logs = wx.getStorageSync('logs') || []
    
    var token = wx.getStorageInfoSync('token');
    var that = this;
    if (!token) {
      this.login()
    }else {
      wx.request({
        url: this.globalData.baseUrl + '/login/validateTokenStatus', 
        method: 'GET',
        data: {
          token: token
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (res) {
          console.log(res)
          var status = res.data.data;
          if (!status) {
            that.login()
          }
        }
      })
    }
  },

login() {
  // 登录
  wx.login({
    success: res => {
      console.log(res)
      wx.request({
          url: this.globalData.baseUrl + '/login/loginOrRegister', 
          method: 'POST',
          data: {
            code: res.code
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res2) {
            var token = res2.data.data
            console.log(token)
            wx.setStorageSync('token', token)
          }
        })
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },

  globalData: {
    userInfo: null,
    baseUrl: 'https://www.jiahaitong.com:80',
    // baseUrl: 'https://127.0.0.1:445'
  }
})
